﻿/************************************************************************
* Copyright (c) 2018 All Rights Reserved.
* 命名空间：ZTService.Bet
* 文件名：CqsscBetAlgorithm
* 创建人：David Rodríguez
* 创建时间：2018/8/6 星期一 11:32:24
* 描述： 重庆时时彩下注算法
*************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using DavidHelper;
using ZTService.Modle;

namespace ZTService.Bet
{
    public class CqsscBetAlgorithm
    {
        private string LastPeriod { get; set; }

        private string LastNumber { get; set; }

        private Timer UserTimer { get; set; } = new Timer();

        public List<BetDataModel> BetList { get; set; }=new List<BetDataModel>();

        public int Uid { get; set; }

        public string Encode { get; set; }

        private bool IsLastSuccess { get; set; }

        private int LastBetMoney { get; set; }

        private static readonly string CheckInUrl = "https://www.yao678.com/API/wis/getBuyNote";


        public CqsscBetAlgorithm()
        {
            StartBet();
        }

        private void StartBet()
        {
            NlogHelper.LogWriter("重庆时时彩智投引擎开启...");
            UserTimer = new Timer
            {
                AutoReset = true,
                Interval = 1000 * 3,
                Enabled = true
            };
            UserTimer.Elapsed += UserTimer_Elapsed;
            UserTimer.Start();
        }

        private void UserTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            //获取上期的期数和号码
            var bet = BetHttpData.GetLastKj(KindId.重庆时时彩);
            if (LastPeriod == bet.Expect)
                return;
            LastPeriod = bet.Expect;
            LastNumber = bet.Opencode;
            foreach (var b in BetList)
            {
                BetSingleModel(b);
            }
        }


        private void BetSingleModel(BetDataModel betData)
        {
            var myLottoryList = new List<BeforeLottory>();
            //投注位置
            if (betData.chooseplayname.Equals(BetIdea.位置投注.ToString()))
            {
                myLottoryList = CheckPos(betData).ToList();
            }
            else if (betData.chooseplayname.Equals(BetIdea.号码投注.ToString()))
            {
                myLottoryList = CheckNumber(betData).ToList();
            }

            //按投注模式
            if (betData.votemode.Equals(Modle.BetModel.常规.ToString()))
            {
                //todo 处理数据
            }
            else if (betData.votemode.Equals(Modle.BetModel.期期滚.ToString()))
            {
                //todo 处理数据
            }

            //翻倍模式
            if (betData.doublemodel.Equals(DoubleModel.一直翻倍.ToString()))
            {
                //todo 处理数据
            }
            else if (betData.doublemodel.Equals(DoubleModel.不中翻倍.ToString()))
            {
                //todo 处理数据
            }
            else if (betData.doublemodel.Equals(DoubleModel.中翻倍.ToString()))
            {
                //todo 处理数据
            }
            //todo 期期滚？

            //球号变换成系统对应的球号ID
            var betList = ConvertLottory(myLottoryList).ToList();
            //下注
            CheckIn(betList);
        }

        public class Temp
        {
            public int uid { get; set; }

            public string buyinfo { get; set; }
        }

        private void CheckIn(List<Lottory> lottoryList)
        {
            var r = new Temp()
            {
                buyinfo = JsonHelper.SerializeObeject(lottoryList).ToBase64String(),
                uid = Uid
            };
            var json = JsonHelper.SerializeObeject(r);
            var re = HttpHelper.Post(CheckInUrl, json, null, "application/json");
        }

        private IEnumerable<Lottory> ConvertLottory(List<BeforeLottory> beforeList)
        {
            foreach (var b in beforeList)
            {
                var posArr = b.BetPos.Split(',');
                foreach (var p in posArr)
                {
                    yield return new Lottory()
                    {
                        de_id = GetNumId(b.Number, p),
                        valem = b.Money
                    };
                }
            }
        }

        private string GetNumId(string num, string pos)
        {
            var headP = "";
            switch (pos)
            {
                case "1":
                    headP = "第一球";
                    break;
                case "2":
                    headP = "第二球";
                    break;
                case "3":
                    headP = "第三球";
                    break;
                case "4":
                    headP = "第四球";
                    break;
                case "5":
                    headP = "第五球";
                    break;
            }
            var total = $"{headP}_{num}";
            var bs = Common.SystemList.FirstOrDefault(x => x.BetEncode == Encode);
            if (bs == null)
            {
                NlogHelper.LogWriter("未找到该用户符合的子系统,请检查encode字段", NlogHelper.LogStatus.Error);
                return "";
            }

            if (bs.CqsscNumberDic.ContainsKey(total))
                return bs.CqsscNumberDic[total];
            else
            {
                NlogHelper.LogWriter("球号系统错误，你整个锤子？", NlogHelper.LogStatus.Error);
                return "";
            }
        }

        /// <summary>
        /// 按位置投注
        /// </summary>
        /// <returns></returns>
        private IEnumerable<BeforeLottory> CheckPos(BetDataModel betModel)
        {
            var numArr = betModel.choosenums;
            var lastNumArr = LastNumber.Split(',');
            foreach (var m in numArr)
            {
                //位置投注，找出上期号码 在相应位置的号码   value1:"1,2,3"
                var posArr = m.value1.Split(',');
                for (var i = 0; i < posArr.Length; i++)
                {
                    var p = 0;
                    if (!Utils.IsNumberic(posArr[i], out p) || p > 5)
                        continue;
                    yield return new BeforeLottory()
                    {
                        Number = lastNumArr[p - 1],
                        Money = betModel.money,
                        BetPos = m.valpos
                    };
                }
            }
        }

        /// <summary>
        /// 按号码投注
        /// </summary>
        /// <returns></returns>
        private IEnumerable<BeforeLottory> CheckNumber(BetDataModel betModel)
        {
            var numArr = betModel.choosenums;
            foreach (var m in numArr)
            {
                //号码投注
                var betNumArr = m.value1.Split(',');
                for (var i = 0; i < betNumArr.Length; i++)
                {
                    var p = 0;
                    if (!Utils.IsNumberic(betNumArr[i], out p) || p > 5)
                        continue;
                    yield return new BeforeLottory()
                    {
                        Number = p.ToString(),
                        Money = betModel.money,
                        BetPos = m.valpos
                    };
                }
            }
        }
    }
}